import Vue from 'vue'
import VueRouter from 'vue-router'
// 分组名生成文件
const Login = () => import(/* webpackChunkName: "login_home_welome" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName: "login_home_welome" */ '../components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "login_home_welome" */ '../components/Welcome.vue')

const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/users/Users.vue')
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Rights.vue')
const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Roles.vue')

const Cate = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Cate.vue')
const Params = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Params.vue')

const GoodsList = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/List.vue')
const Add = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/Add.vue')

const Order = () => import(/* webpackChunkName: "Order_Report" */ '../components/order/Order.vue')
const Report = () => import(/* webpackChunkName: "Order_Report" */ '../components/report/Report.vue')
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/goods', component: GoodsList },
      { path: '/params', component: Params },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report },
      { path: '/goods/add', component: Add },
    ]
  }
]

const router = new VueRouter({
  routes
})
// 为router挂载一个导航守卫
// 其中to 代表去哪 from来自那 next为一个方法决定是否放行
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  } else {
    const totalStr = window.sessionStorage.getItem('token')
    if (!totalStr) {
      return next('/login')
    } else {
      return next()
    }
  }
})

export default router
